package it.nola.soccer;

import it.nola.soccer.adapter.CardAdapter;
import it.nola.soccer.listener.TeamCardItemListener;
import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class CardEdit extends AbstractSoccerActivity {
    private Spinner playerSpinner;
    private Spinner teamSpinner;
    private EditText idCardText;
    private EditText timeText;
    private EditText motivoText;
    private Spinner halfSpinner;
    private Spinner cardSpinner;
    private ListView griglia;
    private TextView titleFormText;
    private TeamCardItemListener teamListener;
    private final String[] fromList=new String[]{Const.Cards.KEY_TEAM, Const.Cards.KEY_NAME_PLAYER,
	Const.Cards.KEY_CARD, Const.Cards.KEY_MIN, Const.Cards.KEY_HALF };
    private final int[] toList=new int[]{R.id.team_card_old, R.id.player_old, R.id.card_old, R.id.min_old,
	R.id.half_old };
    private final String[] fromPlSp=new String[]{Const.Players.KEY_NAME };
    private final int[] toPlSp=new int[]{android.R.id.text1 };

    @Override
    public void cancella(View view) {
	String rowId=idCardText.getText().toString();
	if (!"".equals(rowId)) {
	    dbHelper.cancellaCard(Long.parseLong(rowId));
	    Toast toast=Toast.makeText(this, getString(R.string.card_deleted), Toast.LENGTH_SHORT);
	    toast.show();
	    fillData();
	}
	resetField();
    }

    public void createAndSetPlayerAdapter(int team) {
	Cursor playerCur=dbHelper.getAllPlayers(cronaca, team, false, false);
	startManagingCursor(playerCur);
	SimpleCursorAdapter playerAdapter=new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item,
	    playerCur, fromPlSp, toPlSp);
	playerSpinner.setAdapter(playerAdapter);
    }

    @Override
    protected void fillData() {
	Cursor sc=dbHelper.cercaCard(cronaca);
	startManagingCursor(sc);
	CardAdapter cardAdapter=new CardAdapter(this, R.layout.cards_row, sc, fromList, toList);
	griglia.setAdapter(cardAdapter);
    }

    public Spinner getPlayerSpinner() {
	return playerSpinner;
    }

    @Override
    protected void performSetContentView() {
	setContentView(R.layout.cards_edit);
    }

    @Override
    protected void performSetTitle() {
	setTitle(getText(R.string.cards));
    }

    /**
     * Inizializza i campi presenti nella View
     */
    @Override
    protected void prepareFields() {
	titleFormText=(TextView) CardEdit.this.findViewById(R.id.form_card);
	idCardText=(EditText) findViewById(R.id.id_card);
	motivoText=(EditText) findViewById(R.id.reason);
	playerSpinner=(Spinner) findViewById(R.id.player_card);
	teamSpinner=(Spinner) findViewById(R.id.team_card);
	ArrayAdapter<CharSequence> teamAdapter=new ArrayAdapter<CharSequence>(this,
	    android.R.layout.simple_spinner_item);
	teamAdapter.add(team1Name);
	teamAdapter.add(team2Name);
	teamSpinner.setAdapter(teamAdapter);
	teamListener=new TeamCardItemListener(this);
	teamSpinner.setOnItemSelectedListener(teamListener);
	halfSpinner=(Spinner) findViewById(R.id.half);
	ArrayAdapter<CharSequence> halfAdapter=ArrayAdapter.createFromResource(this, R.array.halfs,
	    android.R.layout.simple_spinner_item);
	halfSpinner.setAdapter(halfAdapter);
	cardSpinner=(Spinner) findViewById(R.id.card);
	ArrayAdapter<CharSequence> cardAdapter=ArrayAdapter.createFromResource(this, R.array.cards,
	    android.R.layout.simple_spinner_item);
	cardSpinner.setAdapter(cardAdapter);
	timeText=(EditText) findViewById(R.id.min);
	griglia=(ListView) findViewById(R.id.gridview);
	griglia.setOnItemClickListener(new OnItemClickListener() {

	    @Override
	    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Cursor c=CardEdit.this.dbHelper.recuperaCard(id);
		startManagingCursor(c);
		if (c.moveToFirst()) {
		    int actTeam=c.getInt(c.getColumnIndex(Const.Cards.KEY_TEAM));
		    CardEdit.this.titleFormText.setText(getString(R.string.edit_card));
		    CardEdit.this.motivoText.setText((c.getString(c.getColumnIndex(Const.Cards.KEY_REASON))));
		    CardEdit.this.halfSpinner.setSelection(c.getInt(c.getColumnIndex(Const.Cards.KEY_HALF)));
		    CardEdit.this.cardSpinner.setSelection(c.getInt(c.getColumnIndex(Const.Cards.KEY_CARD)));
		    CardEdit.this.idCardText.setText(Long.toString(id));
		    CardEdit.this.timeText.setText(c.getString(c.getColumnIndex(Const.Cards.KEY_MIN)));
		    int prevTeam=teamSpinner.getSelectedItemPosition();
		    // se è cambiata la squadra faccio scattare la ricreazione del playerSpinner
		    if (prevTeam!=actTeam) {
			CardEdit.this.teamSpinner.setSelection(actTeam, false);
		    }
		    CardEdit.this.playerSpinner.setSelection(Util.getPlayerPos(
			c.getLong(c.getColumnIndex(Const.Cards.KEY_PLAYER)), CardEdit.this.playerSpinner.getAdapter()));
		}
	    }
	});
    }

    @Override
    protected void resetField() {
	halfSpinner.setSelection(0);
	teamSpinner.setSelection(0);
	playerSpinner.setSelection(0);
	cardSpinner.setSelection(0);
	motivoText.setText("");
	idCardText.setText("");
	timeText.setText("");
	titleFormText.setText(getString(R.string.new_card));
    }

    @Override
    protected void saveState() {
	long player=playerSpinner.getSelectedItemId();
	String rowId=idCardText.getText().toString();
	String motivo=motivoText.getText().toString();
	int team=teamSpinner.getSelectedItemPosition();
	int half=halfSpinner.getSelectedItemPosition();
	int card=cardSpinner.getSelectedItemPosition();
	String min=timeText.getText().toString();
	if (rowId.equals("")) {
	    dbHelper.insertCard(player, team, half, min, cronaca, motivo, card);
	}
	else {
	    dbHelper.updateCard(Long.parseLong(rowId), player, team, half, min, cronaca, motivo, card);
	}
	Toast toast=Toast.makeText(this, getString(R.string.just_saved), Toast.LENGTH_SHORT);
	toast.show();
    }
}
